﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.MasterData
{
    public partial class frmMaintainCommonItemQuery : BaseForm.frmBaseDialogyQueryForm
    {
        public MstCommonItemEntity SelectedCommonItem { get; set; }
        public List<MstCommonItemEntity> SelectedCommonItemList { get; set; }
        private string mItemType;
        private bool mMultiSelected = false;

        public frmMaintainCommonItemQuery(string itemType, bool multi)
        {
            InitializeComponent();
            mItemType = itemType;
            mMultiSelected = multi;
        }

        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindGridComboboxByEnum(colIS_DELETED, typeof(DeleteFlag), typeof(int), true);
        }

        private string GetCondition()
        {
            string condition = " AND ITEM_TYPE = '" + mItemType + "'";
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "ITEM_TYPE, ITEM_CODE";
            DataTable dt = MstCommonItemBs.GetMstCommonItemByCondition(condition, orderBy, "");
            DataColumn dc = new DataColumn("IS_SELECTED", typeof(bool));
            dc.DefaultValue = false;
            dt.Columns.Add(dc);
            gvMain.DataSource = dt;
        }

        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            SelectedCommonItem = new MstCommonItemEntity(dr);
            if ((bool)gridView1.GetDataRow(e.RowHandle)["IS_SELECTED"])
            {
                gridView1.GetDataRow(e.RowHandle)["IS_SELECTED"] = false;
            }
            else
            {
                gridView1.GetDataRow(e.RowHandle)["IS_SELECTED"] = true;
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (SelectedCommonItem != null)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("请选择一个项目！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Confirm()
        {
            if (mMultiSelected)
            {
                SelectedCommonItemList = new List<MstCommonItemEntity>();
                DataTable dt = gvMain.DataSource as DataTable;
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach(DataRow dr in dt.Rows)
                    {
                        if ((bool)dr["IS_SELECTED"])
                        {
                            MstCommonItemEntity entity = new MstCommonItemEntity(dr);
                            SelectedCommonItemList.Add(entity);
                        }
                    }
                }
                if (SelectedCommonItemList.Count == 0)
                {
                    MessageBox.Show("请至少选择一个项目！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                if (SelectedCommonItem == null)
                {
                    MessageBox.Show("请选择一个项目！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                this.DialogResult = DialogResult.OK;
            }
        }


    }
}
